using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCWeb.Models;

namespace MVCWeb.Controllers
{
    public class SelectCommunityController : BaseController
    {
        [Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Index()
        {
            if (Session["CommunityId"] == null)
            {
                ViewData["Message"] = "<br><h2>You must select a community before working on.</h2>";
            }
            else
            {
                Community community = this.dc.Communities.Single(c => c.CommunityId == Convert.ToInt32(Session["CommunityId"]));
                ViewData["Message"] = string.Format("<br><h2>You have selected community: {0}.</h2>", community.Name);
            }

            return View(this.dc.Communities.OrderBy(c => c.Name).ToList<Community>());
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrators, PropertyManagers")]
        public ActionResult Index(FormCollection collection)
        {
            Session["CommunityId"] = Convert.ToInt32(collection["CommunityId"]);
            if (!string.IsNullOrEmpty( collection["ReturnUrl"]))
            {
                return Redirect(collection["ReturnUrl"]);
            }
            return Redirect(Request.RawUrl);
        }
    }
}
